package it.polimi.swimv2.entities;

import java.sql.Timestamp;

import javax.persistence.Column;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;

/**
 * 
 * Request represents a generic request from an user to another within the
 * swimv2 appplication.
 * 
 */

@MappedSuperclass
public abstract class Request {
	
	@Column(name = "Status", nullable = false)
	private RequestStatus status;
	@ManyToOne
	private User sender;
	@ManyToOne
	private User receiver;
	private Timestamp date;
	private boolean visibleToSender=true;
	private boolean visibleToReceiver=true;

	public Request() {
		super();
		this.status = RequestStatus.Pending;
		this.date = new Timestamp(System.currentTimeMillis());
	}

	public Request(User sender, User receiver) {
		this.sender = sender;
		this.receiver = receiver;
		this.status = RequestStatus.Pending;
		this.date = new Timestamp(System.currentTimeMillis());
	}

	/**
	 * @return the sender
	 */
	public User getSender() {
		return sender;
	}

	/**
	 * @param sender
	 *            the sender to set
	 */
	public void setSender(User sender) {
		this.sender = sender;
	}

	/**
	 * @return the receiver
	 */
	public User getReceiver() {
		return receiver;
	}

	/**
	 * @param receiver
	 *            the receiver to set
	 */
	public void setReceiver(User receiver) {
		this.receiver = receiver;
	}

	public RequestStatus getStatus() {
		return status;
	}

	public boolean accept() {
		if (status == RequestStatus.Pending) {
			status = RequestStatus.Accepted;
			return true;
		}
		return false;
	}

	public boolean refuse() {
		if (status == RequestStatus.Pending) {
			status = RequestStatus.Refused;
			return true;
		}
		return false;

	}

	public boolean delete() {
		if (status == RequestStatus.Pending) {
			status = RequestStatus.Deleted;
			return true;
		}
		return false;

	}

	public Timestamp getDate() {
		return date;
	}

	/**
	 * @return the visibleToSender
	 */
	public boolean isVisibleToSender() {
		return visibleToSender;
	}

	/**
	 * @param visibleToSender the visibleToSender to set
	 */
	public void setVisibleToSender(boolean visibleToSender) {
		this.visibleToSender = visibleToSender;
	}

	/**
	 * @return the visibleToReceiver
	 */
	public boolean isVisibleToReceiver() {
		return visibleToReceiver;
	}

	/**
	 * @param visibleToReceiver the visibleToReceiver to set
	 */
	public void setVisibleToReceiver(boolean visibleToReceiver) {
		this.visibleToReceiver = visibleToReceiver;
	}

}
